package glazedlists.examples;

import ca.odell.glazedlists.*;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.matchers.ThreadedMatcherEditor;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Random;

public class FilterListExample {

    public static class MP3 {
        private int track;
        private String song;
        private String album;
        private String artist;

        public MP3(String artist, String album, String name, int track) {
            this.song = name;
            this.album = album;
            this.artist = artist;
            this.track = track;
        }

        public int getTrack() {
            return track;
        }

        public String getSong() {
            return song;
        }

        public String getAlbum() {
            return album;
        }

        public String getArtist() {
            return artist;
        }
    }

    private static final String[] musicalStrings = {"Seven", "Mary", "Three", "Alice", "In", "Chains",
            "Green", "Day", "Led", "Zeppelin", "Beatles", "Prince",
            "Holy", "Cake", "White", "Black", "Sgt.", "Pepper"};

    private static final Random dice = new Random();

    private static String makeRandomMusicString(int numParts) {
        StringBuffer musicBuffer = new StringBuffer();
        for (int i = 0; i < numParts; i++) {
            if (musicBuffer.length() > 0) {
                musicBuffer.append(' ');
            }
            musicBuffer.append(musicalStrings[dice.nextInt(musicalStrings.length)]);
        }
        return musicBuffer.toString();
    }

    public static void main(String[] args) {
        EventList<MP3> mp3s = new BasicEventList<>();
        for (int i = 0; i < 2500; i++) {
            String artist = makeRandomMusicString(2 + dice.nextInt(3));
            String album = makeRandomMusicString(2 + dice.nextInt(3));
            String song = makeRandomMusicString(2 + dice.nextInt(3));
            mp3s.add(new MP3(artist, album, song, i));
        }

        JTextField filterField = new JTextField(25);
        TextFilterator<MP3> mp3TextFilterator = new TextFilterator<MP3>() {
            @Override
            public void getFilterStrings(List<String> list, MP3 mp3) {
                list.add(mp3.getAlbum());
                list.add(mp3.getArtist());
                list.add(mp3.getSong());
            }
        };
        TextComponentMatcherEditor<MP3> matcherEditor = new TextComponentMatcherEditor<MP3>(filterField, mp3TextFilterator);
        FilterList<MP3> filterMP3s = new FilterList<>(mp3s, new ThreadedMatcherEditor<>(matcherEditor));

        JPanel filterPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        filterPanel.add(new JLabel("Filter:"));
        filterPanel.add(filterField);

        // build a JTable
        String[] propertyNames = {"track", "artist", "album", "song"};
        String[] columnLabels = {"Track", "Artist", "Album", "Song"};
        TableFormat tf = GlazedLists.tableFormat(MP3.class, propertyNames, columnLabels);
        JTable table = new JTable(new EventTableModel<>(filterMP3s, tf));

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.add(filterPanel, BorderLayout.NORTH);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
